﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace MattDotson.GlobalToolkit
{
    /// <summary>
    /// This is the master configuration and context information for the global toolkit.
    /// </summary>
    public static class GlobalToolkitContext
    {
        /// <summary>
        /// Initializes the <see cref="GlobalToolkitContext"/> class.
        /// </summary>
        static GlobalToolkitContext()
        {
            CurrencyOptions = new CurrencyOptions();

            GlobalToolkitConfigurationSection configSection = ConfigurationManager.GetSection(GlobalToolkitConfigurationSection.Path) as GlobalToolkitConfigurationSection;
            if (null != configSection)
            {
                if (null != configSection.ServiceLocatorType)
                {
                    ServiceLocator = (IServiceLocator) Activator.CreateInstance(configSection.ServiceLocatorType);
                }

                if (null != configSection.Currency)
                {
                    CurrencyOptions.EnableImplicitConversion = configSection.Currency.EnableImplicitConversion;
                }
            }
        }

        private static IServiceLocator serviceLocator = new DefaultServiceLocator();

        /// <summary>
        /// Gets or sets the service locator which should be used to resolve global toolkit providers.
        /// </summary>
        /// <value>The service locator Global Toolkit should use.</value>
        /// <exception cref="ArgumentNullException">If property is set to null.</exception>
        public static IServiceLocator ServiceLocator
        {
            get
            {
                return serviceLocator;
            }
            set
            {
                Guard.NotNull("value", value);
                serviceLocator = value;
            }
        }

        /// <summary>
        /// Gets the options which determine the behavior of the <see cref="Currency"/> class and other currency related classes.
        /// </summary>
        /// <value>The currency options.</value>
        public static CurrencyOptions CurrencyOptions { get; private set; }

    }

    /// <summary>
    /// Options which control the behavior of the <see cref="Currency"/> class
    /// </summary>
    public sealed class CurrencyOptions
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CurrencyOptions"/> class.
        /// </summary>
        internal CurrencyOptions() { }

        /// <summary>
        /// Gets or sets a value indicating whether the <see cref="Currency"/> class should implicitly convert values 
        /// to other currencies when operations are performed on two <see cref="Currency"/> instances with differing
        /// currency types.  This is turned off by default.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if to enable implicit conversion; otherwise, <c>false</c>.
        /// </value>
        public bool EnableImplicitConversion { get; set; }
    }
}
